<template>
  <a-card :bordered="false">
    <van-search placeholder="请选择条件查询" readonly @click="showSearchPopup" style="margin-top: -10px;width: 100%;padding: 0px;margin-bottom: 10px"/>

    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" style="width: 100%">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="list">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoadList"
        >

          <div
            class="customer-list"
            v-for="(item,i) in dataSource"
            :key="i"
            >
            <div>
              <div class="name">工人姓名：{{item.workerName || ''}}</div>
              <div class="contact">
                <span>电话：{{ item.workerPhone || '无' }}</span>
                <span style="margin-left: 20px;">籍贯：{{ item.workerNativePlace || '无' }}</span>
              </div>
              <div class="contact">
                <span>性别：{{ getDictValue('sex',item.workerSex) }}</span>
                <span style="margin-left: 20px;">工种：{{ item.workerTypeName }}</span>
                <span style="float: right">
                  <a @click="handleEdit(item)">编辑</a>
                  <a-divider type="vertical" />
                  <a @click="workInfoDetail(item)">详情</a>
                </span>
              </div>
              <div class="contact">
                <span>身份证：{{ item.workerIdCard || '无'}}</span>
              </div>
            </div>
          </div>
        </van-list>
      </div>

    </div>
    <van-popup v-model="showSearchPopupFlag" position="top" >
      <a-card :bordered="false">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <div>工人姓名：<a-input placeholder="请输入工人姓名" v-model="queryParam.workerName"></a-input></div>

          <div style="margin-top: 5px">性别：
            <van-dropdown-menu>
              <van-dropdown-item v-model="value1" :options="option1" @change="changeWorkerSex"/>
            </van-dropdown-menu>
          </div>
          <div style="margin-top: 5px">工种：
            <van-dropdown-menu>
              <van-dropdown-item v-model="value2" :options="option2" @change="changeWorkerType"/>
            </van-dropdown-menu>
          </div>
          <a-row :gutter="24" style="margin-bottom: 10px;margin-top: 10px">
            <a-col :span="12">
              <a-button type="primary" style="width: 100%" @click="searchQuery" icon="search">查询</a-button>
            </a-col>
            <a-col :span="12">
              <a-button type="primary" @click="searchReset" icon="reload" style="width: 100%">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </a-card>
    </van-popup>
    <gh-worker-info-modal ref="modalForm" @ok="modalFormOk"></gh-worker-info-modal>
    <work-info-detail-modal ref="WorkInfoDetailModal"></work-info-detail-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import GhWorkerInfoModal from './modules/GhWorkerInfoModal'
  import {filterDictTextByCache} from '@comp/dict/JDictSelectUtil'
  import {getAction} from "../../../../api/manage";
  import WorkInfoDetailModal from './modules/workInfoDetailModal'

  export default {
    name: 'workerInfoList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      WorkInfoDetailModal,
      GhWorkerInfoModal
    },
    data () {
      return {
        description: '工人信息表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'姓名',
            align:"center",
            dataIndex: 'workerName'
          },
          {
            title:'性别',
            align:"center",
            dataIndex: 'workerSex',
            customRender: (text) => {
              //字典值翻译通用方法
              return filterDictTextByCache('sex', text);
            }
          },
          {
            title:'工种',
            align:"center",
            dataIndex: 'workerTypeName',
          },
          {
            title:'手机号',
            align:"center",
            dataIndex: 'workerPhone'
          },
          {
            title:'身份证',
            align:"center",
            dataIndex: 'workerIdCard'
          },
          {
            title:'籍贯',
            align:"center",
            dataIndex: 'workerNativePlace'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/GhWorkerInfo/list",
          delete: "/GhWorkerInfo/delete",
          deleteBatch: "/GhWorkerInfo/deleteBatch",
          exportXlsUrl: "/GhWorkerInfo/exportXls",
          importExcelUrl: "/GhWorkerInfo/importExcel",
          loadWorkerTypeDataList: "/ghKindOfWork/queryList",
        },
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        workerTypeData:[],
        /* 查询条件-请不要在queryParam中声明非字符串值的属性 */
        queryParam: {},
        dictOptions:{},
        selectedRowKeys: [],
        selectedRows: [],
        finished: false,
        loading: false,
        showSearchPopupFlag: false,
        value1: "",
        option1: [
          { text: '全部', value: "" },
          { text: '男', value: 1 },
          { text: '女', value: 2 },
        ],
        value2: "",
        option2: [
          {text:'全部',value:""}
        ],
      }
    },
    created() {
      this.loadWorkerTypeData();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      showSearchPopup(){
        this.showSearchPopupFlag = true
      },
      searchQuery(){
        this.loadData(1);
        this.selectedRowKeys = []
        this.selectedRows = []
        this.showSearchPopupFlag = false
      },
      searchReset() {
        this.value1 = ""
        this.value2 = ""
        this.queryParam = {}
        this.selectedRowKeys = []
        this.selectedRows = []
        this.showSearchPopupFlag = false
        this.loadData(1);
      },
      changeWorkerSex(value){
        this.showSearchPopupFlag = false
        this.queryParam.workerSex = value
        this.loadData(1);
      },
      loadWorkerTypeData(){
        getAction(this.url.loadWorkerTypeDataList).then((res) => {
          if (res.success) {
            this.workerTypeData = res.result.records||res.result;
            for (let i=0;i<this.workerTypeData.length;i++){
              this.option2.push({
                text: this.workerTypeData[i].kindOfWork,
                value: this.workerTypeData[i].id
              })
            }
          }else{
            this.$message.warning(res.message)
          }
        })
      },
      filterOptionWorkerType(input,option){
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      changeWorkerType(value){
        this.showSearchPopupFlag = false
        this.queryParam.workerType = value
        this.loadData(1);
      },
      workInfoDetail(record){
        this.$refs.WorkInfoDetailModal.title = "工人详情"
        this.$refs.WorkInfoDetailModal.showDrawer(record);
      },
      onSelectAll(selected, selectedRows, changeRows) {
        if(selected) {
          let rows = [...selectedRows]
          this.selectedRows = this.selectedRows.concat(rows)
        } else {
          changeRows.forEach(item => {
            const delIndex = this.selectedRows.findIndex((val) => {
              return val.id === item.id
            })
            this.selectedRows.splice(delIndex, 1)
          })
        }
      },
      onSelect(record, selected) {
        if(selected) {
          this.selectedRows.push(record);
        } else {
          const delIndex = this.selectedRows.findIndex((val) => {
            return val.id === record.id
          })
          this.selectedRows.splice(delIndex,1)
        }
      },
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys;
      },
      onLoadList(e){
        this.ipagination.current++
        this.loadData(this.ipagination.current)
      },
      loadData(arg) {
        if (!this.url.list) {
          this.$message.error("请设置url.list属性!")
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
          this.dataSource = []
          this.finished = false;
        }
        var params = this.getQueryParams();//查询条件
        this.loading = true;
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
            this.dataSource = [...this.dataSource, ...res.result.records]
            if (res.result.total) {
              this.ipagination.total = res.result.total;
            } else {
              this.ipagination.total = 0;
            }
            //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          } else {
            this.$message.warning(res.message)
          }
          if(res.result.records.length<10){
            this.finished = true;
          }
        }).finally(() => {
          this.loading = false
        })
      },
      getDictValue(code,value){
        if (value != null){
          return filterDictTextByCache(code,value)
        }else {
          return "无"
        }
      },
      modalFormOk(){
        this.loadData(1)
      },
    }
  }
</script>
<style scoped lang="less">
  @import '~@assets/less/common.less';
  /deep/ .van-dropdown-menu__bar{
    height: 30px;
    box-shadow: none;
    border: 1px solid lightgrey;
    border-radius: 5px;
  }
  .select-pk{
    display:flex;
    align-items:center;
    margin:10px 0;
    &>span{
      font-weight: bold;
    }
    &-text{
      width: calc(100% - 6em);
      height: 30px;
      text-indent: 1em;
      line-height: 28px;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
    }
  }
  .list{
    overflow-y: scroll;
    height:100vh;
    margin-top: 10px;
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .customer-list{
    /*display: flex;*/
    align-items: center;
    border-top: 1px solid #d9d9d9;
    .name{
      font-size: 13px;
      color: #161616;
      line-height: normal;
      margin-top: 5px;
    }
    .contact{
      margin-top: 5px;
      font-size: 12px;
      color: #a39f9f;
      margin-bottom: 5px;
    }
  }
</style>